package cn.iune.erp.sale;


import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;

@Transactional
@Service
public class SaleOrderDetailServiceImpl implements ISaleOrderDetailService {

	@Autowired
	private ISaleOrderDetailDao dao;
//	@Autowired
//	private ISaleOrderService saleOrderService;
		
	
	public SaleOrderDetailServiceImpl(){

	}
	@Override
	public List<SaleOrderDetail> getAll(){
		return dao.getAll();
	}
	@Override
	public SaleOrderDetail getByID(String fID) {
		return dao.getByID(fID);
	}
	
	@Transactional
	@Override
	public void insert(SaleOrderDetail obj) {
		dao.insert(obj);
	}
	
	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}
	
	@Transactional
	@Override
	public void update(SaleOrderDetail obj) {
		dao.update(obj);
	}

	@Override
	public SaleOrderDetail getByNo(String orderNo, String serialNo, String enterpriseNo) {
		return dao.getByNo(orderNo, serialNo, enterpriseNo);
	}
	
	@Override
	public List<SaleOrderDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}
	
	@Override
	public List<SaleOrderDetail> getByCustomerID(String CustomerID) {
		return dao.getByCustomerID(CustomerID);
	}
	
//	@Override
//	public List<SaleOrderDetail> getByGuestNumber(String guestNumber, String enterpriseNo) {
//		return dao.getByGuestNumber(guestNumber, enterpriseNo);
//	}
	

	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException  {
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		
		JSONObject data = new JSONObject();
		data.put("concept", table.getTableName());
//		JSONArray rows = new JSONArray();
//		List<String> idsNew = new ArrayList<String>();
//		List<String> idsDel = new ArrayList<String>();
		
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			SaleOrderDetail obj = JSON.toJavaObject(javaObject, SaleOrderDetail.class);
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
//				idsNew.add(obj.getfID());
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
//				idsDel.add(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				dao.update(obj);
				//返回+1后的version到前台BizData中进行刷新，使得前台的version与后台的一致。
				///////////////////////////////////////////////////////////////////////
//				JSONObject jsonRow = new JSONObject();
//				jsonRow.put("id", obj.getfID());
//				jsonRow.put("version", objNew.getVersion());
//				rows.add(jsonRow);
				//////////////////////////////////////////////////////////////////////
			}
		}
		
//		data.put("rows", rows);
//		data.put("idsNew", idsNew);
//		data.put("idsDel", idsDel);
		
		data.put("code", 0);
		data.put("msg", "success");
		return data;
	}
	
	@Override
	public JSONObject querySimple(JSONObject data) {
		try {
			return dao.querySimple(data);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", "failure");
			return json;
		}
	}


	


	
	///////////////////////业务功能///////////////////////////////////////

	
	@Override
	public List<SaleOrderDetail> getByStorageNo(String storageNo, String factoryNo, String enterpriseNo) {
		return dao.getByStorageNo(storageNo, factoryNo, enterpriseNo);
	}
	
	
	@Transactional
	@Override
	public int confirm(String fID) {
		SaleOrderDetail sod = dao.getByID(fID);
		return this.confirm(sod);
	}
	
	@Transactional
	@Override
	public int active(String fID) {
		SaleOrderDetail sod = dao.getByID(fID);
		return this.active(sod);
	}
	
	@Transactional
	@Override
	public int confirm(SaleOrderDetail sod) {
		if(sod.getState().equals("confirm")) return 0;
		sod.setState("confirm");
		sod.setResultStatus("审核中");
		dao.update(sod);
		return 0;
	}
	
	@Transactional
	@Override
	public int active(SaleOrderDetail sod) {
		sod.setState("active");
		sod.setResultStatus("待审核");
		dao.update(sod);
		return 0;
	}

	@Transactional
	@Override
	public int deleteByMasterID(String fMasterID){
		List<SaleOrderDetail> srds = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail srd:srds){
			dao.delete(srd.getfID());;
		}
		return 0;
	}

	@Transactional
	@Override
	public int confirmByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod:sods){
			this.confirm(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int activeByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod:sods){
			this.active(sod);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int open(String fID) {
		SaleOrderDetail sod = dao.getByID(fID);
		return this.open(sod);
	}
	
	@Transactional
	@Override
	public int close(String fID) {
		SaleOrderDetail sod = dao.getByID(fID);
		return this.close(sod);
	}
	
	@Transactional
	@Override
	public int open(SaleOrderDetail sod) {
		sod.setStatus("open");
		dao.update(sod);
		return 0;
	}
	
	@Transactional
	@Override
	public int close(SaleOrderDetail sod) {
		sod.setStatus("close");
		dao.update(sod);
		return 0;
	}
	
	@Transactional
	@Override
	public int cancel(SaleOrderDetail sod) {
		sod.setStatus("cancel");
		dao.update(sod);
		return 0;
	}

	@Transactional
	@Override
	public int openByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod:sods){
			this.open(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int closeByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod:sods){
			this.close(sod);
		}
		return 0;
	}
	
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(SaleOrderDetail s) {
		if(s.getAudit().equals("confirm")) return 0;
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		s.setAudit("confirm");
		s.setConfirmDate(Utils.getCurrentDate());
		s.setResultStatus("已审核");

		dao.update(s);
		
		return 0;
	}
	
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(String fID) {
		SaleOrderDetail s = dao.getByID(fID);
		return this.agreeIt(s);
	}

	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(SaleOrderDetail s) {
		//更新数据库工具入库单的单据状态
		s.setAudit("active");
		s.setConfirmDate(null);
		s.setResultStatus("审核中");
		dao.update(s);
		
		return 0;
	}
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(String fID) {
		SaleOrderDetail s = dao.getByID(fID);
		return this.unAgree(s);
	}

	@Transactional
	@Override
	public int agreeItByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod :sods){
			this.agreeIt(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int unAgreeByMasterID(String fMasterID){
		List<SaleOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(SaleOrderDetail sod :sods){
			this.unAgree(sod);
		}
		return 0;
	}

	

	/*
	@Transactional
	@Override
	public int updatePlaceOrder(String orderDetailNo ,String enterpriseNo,String placeOrder	){
		
		SaleOrderDetail s = dao.getByNo(orderDetailNo, enterpriseNo);
		if(s==null) return 1;
		//新值保留
		s.setPlaceOrder(placeOrder);
		dao.update(s);
		
		return 0;
	}
	*/
	
	@Override
	public SaleOrderDetail packing(SaleOrder saleOrder){
		SaleOrderDetail obj = new SaleOrderDetail();
		String fID = Utils.getUUID();
		obj.setfID(fID);
		
		obj.setOrderType(saleOrder.getOrderType());
		obj.setOrderTypeLabel(saleOrder.getOrderTypeLabel());
		
		obj.setBillType(saleOrder.getBillType());
		obj.setBillTypeName(saleOrder.getBillTypeName());
		obj.setSystemCode(saleOrder.getSystemCode());
//		obj.setRevOpay(saleOrder.getRevOpay());
//		obj.setDirection(saleOrder.getDirection());
//		obj.setCostingCode(saleOrder.getCostingCode());
		obj.setOrigin(saleOrder.getOrigin());
		obj.setOriginName(saleOrder.getOriginName());
		
		obj.setState(saleOrder.getState());
		obj.setStatus(saleOrder.getStatus());
		obj.setAudit(saleOrder.getAudit());
		obj.setOrderDate(saleOrder.getOrderDate());
		
		obj.setfMasterID(saleOrder.getfID());
		obj.setOrderNo(saleOrder.getOrderNo());
		//子表明细号
		int maxDetailNo = saleOrder.getMaxDetailNo() + 1;
		String serialNo = String.format("%05d", maxDetailNo);
		obj.setSerialNo(serialNo);
		
		obj.setFactoryID(saleOrder.getFactoryID());
		obj.setFactoryNo(saleOrder.getFactoryNo());
		obj.setFactoryName(saleOrder.getFactoryName());
		
		
		obj.setStorageType(saleOrder.getStorageType());
		obj.setStorageTypeLabel(saleOrder.getStorageTypeLabel());
		obj.setStorageID(saleOrder.getStorageID());
		obj.setStorageNo(saleOrder.getStorageNo());
		obj.setStorageName(saleOrder.getStorageName());
		obj.setFloorNo(saleOrder.getFloorNo());

		
		obj.setEmployeeNo(saleOrder.getEmployeeNo());
		obj.setEmployeeName(saleOrder.getEmployeeName());
		
		obj.setStorageTypeD(saleOrder.getStorageTypeD());
		obj.setStorageTypeDLabel(saleOrder.getStorageTypeDLabel());
		obj.setCustomerID(saleOrder.getCustomerID());
		obj.setCustomerNo(saleOrder.getCustomerNo());
		obj.setShortName(saleOrder.getShortName());
		obj.setCustomerName(saleOrder.getCustomerName());
		
		obj.setTaxType(saleOrder.getTaxType());
		obj.setTaxTypeLabel(saleOrder.getTaxTypeLabel());
		obj.setTaxRate(saleOrder.getTaxRate());

//		obj.setPickItemIng(BigDecimal.ZERO);
//		obj.setPickItemAmount(BigDecimal.ZERO);
//		obj.setPickPiece(BigDecimal.ZERO);
//		obj.setBackItemIng(BigDecimal.ZERO);
//		obj.setBackItemAmount(BigDecimal.ZERO);
//		obj.setBackPiece(BigDecimal.ZERO);
//		
//		obj.setPieceQuantity(BigDecimal.ZERO);
//
//		obj.setItemWeight(BigDecimal.ZERO);
//		obj.setOrderWeight(BigDecimal.ZERO);
		
		obj.setcUsercode(saleOrder.getcUsercode());
		obj.setcUsername(saleOrder.getcUsername());
		obj.setcDatetime(saleOrder.getcDatetime());
		
		obj.setUserId(saleOrder.getUserId());
		obj.setEnterpriseNo(saleOrder.getEnterpriseNo());
		obj.setEnterpriseName(saleOrder.getEnterpriseName());
		
		obj.setVersion(0);
		
		return obj;
	}
    

}